//
//  XMGFoundTVc.m
//  weibo
//
//  Created by DP on 16/7/3.
//  Copyright © 2016年 DP. All rights reserved.
//

#import "XMGFoundTVc.h"
#import "XMGFoundCell.h"
#import "XMGHotCell.h"
#import "CustomScrollViews.h"
#import "XMGHotWBVc.h"
#import "XMGFoundIGroupItem.h"
#import "XMGFoundItem.h"
#import "XMGFindPeopleVc.h"
#import "XMGMoreVc.h"
#import <MJExtension.h>



#define kScreenWidth  self.tableView.frame.size.width  // 轮播器的宽度
#define kScreenHeight   88 //轮播器的高度


@interface XMGFoundTVc ()

//数据源
@property(nonatomic, strong)NSArray *dataGroup;
@end

//定义一个标识符
static NSString *ID = @"fonud";
static NSString *IP = @"hot";

@implementation XMGFoundTVc

//懒加载数据
-(NSArray *)dataGroup{
    if(_dataGroup == nil){
//    告诉MJExtension这个框架XMGFoundIGroupItem的group数组属性中装的是XMGFoundItem这种类型的对象
        [XMGFoundIGroupItem mj_setupObjectClassInArray:^NSDictionary *{
            return @{@"group" : [XMGFoundItem class]};
        }];
    
        _dataGroup = [XMGFoundIGroupItem mj_objectArrayWithFilename:@"found.plist"];
    
    }
    return _dataGroup;

}



- (void)viewDidLoad {
    [super viewDidLoad];
         //    导航控制器的scrollView默认会有一个向下的滚动区域 64；
    self.automaticallyAdjustsScrollViewInsets = NO;
//    让tableVeiw自动有有一个向下滚动的区域60
    self.tableView.contentInset =  UIEdgeInsetsMake(20, 0, 0, 0);
    
    //注册一个foundcell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGFoundCell class]) bundle:nil] forCellReuseIdentifier:ID];
//    注册一个hotcell
     [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGHotCell class]) bundle:nil] forCellReuseIdentifier:IP];
    
/*************对tableView进行基本属性的设置******************/
//    设置tableView的表头控件为一个轮播器
 
    UIImage *image1 = [UIImage imageNamed:@"img_01"];
    UIImage *image2 = [UIImage imageNamed:@"img_02"];
    UIImage *image3 = [UIImage imageNamed:@"img_03"];
    NSArray *array = [NSArray arrayWithObjects:image1, image2, image3, nil];
    CustomScrollViews *scrollView = [[CustomScrollViews alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 130) withImage:array withLabelText:nil];
    self.tableView.tableHeaderView = scrollView;
    
   //    设置tableView的表尾控件
    UIView *footView = [[UIView alloc]init];
    footView.frame = CGRectMake(0, self.tableView.frame.size.height - 57, kScreenWidth, 57);
    self.tableView.tableFooterView = footView;
    
//    取消tableView下拉时的弹框效果
    self.tableView.bounces = NO;
//    设置tableVie的每一行的高度
    self.tableView.rowHeight = 50;
//    设置tableView的分割线
    self.tableView.separatorColor = [UIColor lightGrayColor];
//    隐藏tableView的滚动条
    self.tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView.sectionHeaderHeight = 5;
    
    
}


#pragma mark - Tableviewdatasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataGroup.count;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(section ==0||section ==1||section ==2)
    {
        return 2;
    }else {
        XMGFoundIGroupItem *groupItem  = self.dataGroup[section];
        return groupItem.group.count;
    }
     
    
   
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
    
        XMGHotCell *cell = [tableView dequeueReusableCellWithIdentifier:IP];
        
        return cell;
    }
    else{
    //访问缓冲池
    XMGFoundCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        XMGFoundIGroupItem *groupItem = self.dataGroup[indexPath.section];
        XMGFoundItem *foundItem = groupItem.group[indexPath.row];
        cell.Foundtem = foundItem;
        
    return cell;
    }
}


//设置组与组之间的间距
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }else
    {
        return 8;
    }
}

/*********点击开始控制器的跳转***********/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%zd组-----%zd行",indexPath.section, indexPath.row);
   
        /**点击热门微博跳转到hotVc控制器*/
    if(indexPath.section == 1 ){
        if(indexPath.row ==0){
        XMGHotWBVc *hotWBVc = [[XMGHotWBVc alloc]init];
            [self.navigationController pushViewController:hotWBVc animated:YES];
        }else if(indexPath.row == 1){
            XMGFindPeopleVc *findPPVc = [[XMGFindPeopleVc alloc]init];
            [self.navigationController pushViewController:findPPVc animated:YES];
        
        }
    }else if(indexPath.section == 3 && indexPath.row == 8){

            XMGMoreVc *moreVc = [[XMGMoreVc alloc]init];
            [self.navigationController pushViewController:moreVc animated:YES];
        moreVc.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    }
    
    
}

//当tableView显示的时候让navigationBar隐藏
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;

}




@end
